package com.tenglong.teacher.data.resultInfo.base;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import java.net.ConnectException;

import retrofit2.HttpException;
import retrofit2.Response;

public class ResultVerifier {
    public static <T> boolean isSucceed(Response<BaseResult<T>> response) {
        if (response.body() == null) return false;
        BaseResult baseModel = response.body();
        if (response.isSuccessful() &&response.code()==200&& baseModel != null) {
            if (baseModel.status == 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }


//    public static <T> boolean isSucceed(Response<BaseResult<T>> response) {
//        if (response.body() == null) return false;
//        boolean ok = isOk(null, response.code(), response.body().message);
//        if (ok){
//            BaseResult baseModel = response.body();
//            if (baseModel != null) {
//                return isOk(null,baseModel.status,baseModel.message);
//            } else {
//                Log.e("errer", baseModel == null ? "数据结构错误" : (baseModel.message == null ? "baseModel.message==null" : baseModel.message));
//                return false;
//            }
//        } else {
//            return false;
//        }
//    }


    public static <T> String serceErrers(Response<BaseResult<T>> response) {
        return response.body() == null ? "错误码：" + response.code() : (response.body().message == null ? "response.body().message===null" : response.body().message);
    }


    /**
     * 这个方法时已经成功访问后台了，code是后台约定的错误码，判断访问是否成功
     *
     * @param context 在做异常处理的时候可能涉及到跳转Activity
     * @return 返回成功或失败
     */
    public static boolean isOk(Context context, int status, String message) {
        if (status == 0 || status == 200) {
            return true;
        } else {
            NetworkError.error(new ServerException(status, message));
            return false;
        }
    }

    public static void systerErrers(Throwable t) {
        NetworkError.error( t);
        String message = t.getMessage();
        if (message != null) {
//            if (t instanceof ConnectException) {
//                //网络连接失败
//                Log.e("errer",t.getMessage());
//            } else if (t instanceof HttpException) {
//                HttpException ex = (HttpException) t;
//                onFailed(ex.code(), ex.message());
//            } else {
//                onFailed(405, t.getMessage());
//            }
        }
    }
}
